package ta.utils;

import java.util.List;

public class DaoGenerator
{
	static String rt = "\r\t";
	// 创建Dao类
	public static void createDao(List<String> tablelist)
	{
		for (String tablename : tablelist)
		{
			String className=toFirstUpperCase(tablename);
			List<String[]> colsList = TableMeta.getColumnDatas(tablename);
			StringBuilder addstr=new StringBuilder("insert into "+tablename+"(");			
			StringBuilder parastr=new StringBuilder("Object[] params={");
			StringBuilder ins=new StringBuilder();
			for(String[] coldata:colsList)
			{				
				parastr.append(tablename+".get"+toFirstUpperCase(coldata[0])+"(),");
				addstr.append(coldata[0]+",");	
				ins.append("?,");
			}
			addstr.deleteCharAt(addstr.length()-1);
			ins.deleteCharAt(ins.length()-1);
			parastr.deleteCharAt(parastr.length()-1);
			parastr.append("};");
			addstr.append(") values(");
			addstr.append(ins+")");
			StringBuilder sb = new StringBuilder();
			sb.append("package " + CodeConfig.DaoPackageName + ";\n");
			sb.append("import java.util.ArrayList;\r\n" + "import java.util.List;\r\n" + "\r\n" + "import "
					+ CodeConfig.BeanPackageName + "." + className + ";\r\n"
					+ "import yzh.tools.JDBCUtils;\n"+ "import yzh.tools.PageInfo;\n");
			// 创建类，类名为表名首字母大写
			sb.append("public class " + className + "Dao{" + rt);
			if(tablename.contains("user"))
			{
				//login
				sb.append("//login\r\n"
						+ "\tpublic boolean login("+className +" "+tablename+")\r\n"
						+ "\t{			\r\n"
						+ "\t\tString sql=\"select * from "+tablename+" where "+colsList.get(1)[0]+"=? and "+colsList.get(2)[0]+"=?\";		\r\n"
						+ "\t\tObject[] params= {"+tablename+".get"+toFirstUpperCase(colsList.get(1)[0])+"(),"+tablename+".get"+toFirstUpperCase(colsList.get(2)[0])+"()};\r\n"
						+ "\t\treturn JDBCUtils.getBean(sql,params,"+className+".class) != null;\r\n"
						+ "\t}"+rt);
				
				//login
				sb.append("//login\r\n"
						+ "\tpublic boolean login(String username,String userpwd)\r\n"
						+ "\t{			\r\n"
						+ "\t\tString sql=\"select * from "+tablename+" where "+colsList.get(1)[0]+"=? and "+colsList.get(2)[0]+"=?\";		\r\n"
						+ "\t\tObject[] params= {username,userpwd};\r\n"
						+ "\t\treturn JDBCUtils.getBean(sql,params,"+className+".class) != null;\r\n"
						+ "\t}"+rt);
			}
			//add
			sb.append("//add\r\n"
					+ "\tpublic boolean add(" + className +" "+tablename+")\r\n"
					+"\t{"+rt+ "\tString sql=\""
					+addstr+"\";"+ rt
					+"\t"+parastr+ rt+ "\treturn JDBCUtils.update(sql, params);\r\n"
					+ "\t}\r\n");
			//del
			sb.append("\t//del	\r\n"
					+ "\tpublic boolean del(String "+colsList.get(0)[0]+")\r\n"
					+ "\t{			\r\n"
					+ "\t\tString sql=\"delete from "+tablename+" where "+colsList.get(0)[0]+"=?\";\r\n"
					+ "\t\tObject[] params= {"+colsList.get(0)[0]+"};\r\n"
					+ "\t\treturn JDBCUtils.update(sql, params);\r\n"
					+ "	}" + rt);
		
			//getbyid
			sb.append("//getById\r\n"
					+ "\tpublic "+className+" getById(String "+colsList.get(0)[0]+")\r\n"
					+ "\t{			\r\n"
					+ "\t\tString sql=\"select * from "+tablename+" where "+colsList.get(0)[0]+"=?\";		\r\n"
					+ "\t\tObject[] params= {"+colsList.get(0)[0]+"};\r\n"
					+ "\t\treturn JDBCUtils.getBean(sql,params,"+className+".class);\r\n"
					+ "\t}"+rt);
			//modify
			sb.append("//modify\r\n"
					+ "\tpublic boolean edit("+className+" "+tablename+")\r\n"
					+ "\t{\r\n"
					+ "\t\tStringBuilder str=new StringBuilder(\"update userinfo set \");\r\n"
					+ "\t\tList<Object> list=new ArrayList<Object>();\r\n");
			for(int i=1;i<colsList.size();i++)
			{
			sb.append("\t\tif("+tablename+".get"+toFirstUpperCase(colsList.get(i)[0])+"()!=null)\r\n"
					+ "\t\t{\r\n"
					+ "\t\t\tstr.append(\""+colsList.get(i)[0]+"=?,\");\r\n"
					+ "\t\t\tlist.add("+tablename+".get"+toFirstUpperCase(colsList.get(i)[0])+"());\r\n"
					+ "\t\t}\r\n");
			}
			sb.append("\t\tstr.deleteCharAt(str.length()-1);			\r\n"
					+ "\t\tstr.append(\" where "+colsList.get(0)[0]+"=?\");			\r\n"
					+ "\t\tlist.add("+tablename+".get"+toFirstUpperCase(colsList.get(0)[0])+"());\r\n"
					+ "\t\tObject[] params= list.toArray();\r\n"
					+ "\t\treturn JDBCUtils.update(str.toString(), params);\r\n"
					+ "\t}" + rt);
			//search
			sb.append("//getAll\r\n");
			sb.append("\tpublic void getAll(String keyword, PageInfo<"+className+"> pb)\r\n"
					+ "	{\r\n"
					+ "		String sql1 = \"SELECT COUNT(1) FROM "+tablename+" WHERE (1>2 ");
			StringBuilder s1=new StringBuilder();
			StringBuilder s2=new StringBuilder();
			for(int i=0;i<colsList.size();i++)
			{
			   s2.append(" or "+colsList.get(i)[0]+" LIKE CONCAT('%',?,'%')");
			   s1.append("keyword,");
			}
			s1.deleteCharAt(s1.length()-1);
			sb.append(s2);
			sb.append(" ) \";\r\n	\tObject[] params1 ={ "+s1);
			//+keyword, keyword, keyword +
			       sb.append("};\r\n"					
					+ "		long total=(long) JDBCUtils.getScalar(sql1, params1);     	\r\n"
					+ "    	int totalpage=(int)(total+pb.getPagesize()-1)/pb.getPagesize();    	\r\n"
					+ "		String sql = \"select * from "+tablename+" where (1>2 "+s2+") order by "+colsList.get(0)[0]+" desc limit ?,?\";\r\n"
					+ "		Object[] param ={ "+s1+", (pb.getCurrentpage() - 1) * pb.getPagesize(), pb.getPagesize() };\r\n"
					+ "		List<"+className+"> list = JDBCUtils.getList(sql, param, "+className+".class);\r\n"
					+ "\t    pb.setTotalpage(totalpage);\r\n"
					+ "		pb.setData(list);\r\n"
					+ "	}"+ rt);
			sb.append("\n}" + rt);
			// 创建文件
			FileHelper.genModuleTpl(
					CodeConfig.getRoot(CodeConfig.daoPath + "\\" + className + "Dao.java"),
					sb.toString());
			System.out.println(CodeConfig.DaoPackageName + ": " + tablename + "Dao创建成功!");

		}
	}

	/**
	 * 首字母大写
	 */
	public static String toFirstUpperCase(String s)
	{
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}
	
	public static void main(String[] args)
	{
		List<String> list=TableMeta.getTables();		
		createDao(list);
	}

}
